package com.godfrey.poi.util;


import com.alibaba.excel.EasyExcel;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import com.godfrey.poi.pojo.RectangleCoordinate;
import com.godfrey.poi.pojo.Street;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author godfrey
 * @since 2021-12-05
 */
public class ParseJsonUtil {
    public static void main(String[] args) throws Exception {
        JsonFactory f = new MappingJsonFactory();
        List<Street> streetsList = new ArrayList<>();
        JsonParser jp = f.createJsonParser(new File("F:/featuresToJSON.json"));
        JsonToken current;
        current = jp.nextToken();
        if (current != JsonToken.START_OBJECT) {
            System.out.println("Error: root should be object: quiting.");
            return;
        }
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            // move from field name to field value
            current = jp.nextToken();
            if ("features".equals(fieldName)) {
                if (current == JsonToken.START_ARRAY) {
                    // For each of the records in the array
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        // read the record into a tree model,
                        // this moves the parsing position to the end of it
                        JsonNode node = jp.readValueAsTree();
                        // And now we have random access to everything in the object
                        String provinc = node.get("attributes").get("省").asText();
                        String city = node.get("attributes").get("市").asText();
                        String country = node.get("attributes").get("县").asText();
                        String town = node.get("attributes").get("乡").asText();
                        String border = node.get("geometry").get("rings").get(0).toString();
                        String rectangle = getRectangle(border);
                        streetsList.add(new Street(provinc, city, country, town, rectangle));
                    }
                } else {
                    System.out.println("Error: records should be an array: skipping.");
                    jp.skipChildren();
                }
            } else {
                System.out.println("Unprocessed property: " + fieldName);
                jp.skipChildren();
            }
        }
        System.out.println("街道数据读取成功");
        EasyExcel.write(System.getProperty("user.dir").replace("gaode_poi", "") + "street.xlsx", Street.class).sheet("街道数据表").doWrite(streetsList);
        System.out.println("街道数据写入Excel成功");
    }

    /**
     * 获取行政区边界范围矩形
     *
     * @param coordinates 行政区边界坐标
     * @return com.godfrey.poi.pojo.RectangleCoordinate
     */
    private static String getRectangle(String coordinates) {
        coordinates = coordinates.replace("],[", ";").replace("[[", "").replace("]]", "");
        String[] split = coordinates.split(";");
        //经度
        List<Double> longitude = new ArrayList<>();
        //维度
        List<Double> latitude = new ArrayList<>();
        Arrays.stream(split).forEach(e -> {
            String[] s = e.split(",");
            longitude.add(Double.parseDouble(s[0]));
            latitude.add(Double.parseDouble(s[1]));
        });
        //获得矩形区域
        return new RectangleCoordinate(Collections.min(longitude), Collections.max(latitude), Collections.max(longitude), Collections.min(latitude)).getPolygonString();
    }
}